{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Building a Linkup Data Agent\n",
    "\n",
    "This tutorial walks through using the LLM tools provided by the [Linkup API](https://app.linkup.so/) to allow LLMs to easily search and retrieve relevant content from the Internet.\n",
    "\n",
    "To get started, you will need an [OpenAI api key](https://platform.openai.com/account/api-keys) and a [Linkup API key](https://app.linkup.so)\n",
    "\n",
    "We will import the relevant agents and tools and pass them our keys here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mRunning cells with '.venv (Python 3.13.1)' requires the ipykernel package.\n",
      "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
      "\u001b[1;31mCommand: '/Users/juliettesivan/llama_index/.venv/bin/python -m pip install ipykernel -U --force-reinstall'"
     ]
    }
   ],
   "source": [
    "%pip install llama-index-tools-linkup-research llama-index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set your openai key, if using openai\n",
    "\n",
    "import os\n",
    "from llama_index.llms.openai import OpenAI\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set up Linkup tool\n",
    "\n",
    "from llama_index.tools.linkup_research.base import LinkupToolSpec\n",
    "\n",
    "# structured_schema=json.dumps(your schema here) # Only if output type is structured\n",
    "# Initialisation of the tool\n",
    "linkup_tool = LinkupToolSpec(\n",
    "    api_key=\"your Linkup API Key\",\n",
    "    depth=\"\",  # Choose (standard) for a faster result (deep) for a slower but more complete result.\n",
    "    output_type=\"\",  # Choose (searchResults) for a list of results relative to your query, (sourcedAnswer) for an answer and a list of sources, or (structured) if you want a specific shema.\n",
    "    # structured_output_schema=structured_schema # Only if output type is structured\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Call the agent\n",
    "\n",
    "from llama_index.core.agent import FunctionAgent\n",
    "\n",
    "agent = FunctionAgent(\n",
    "    tools=linkup_tool.to_tool_list(),\n",
    "    llm=OpenAI(model=\"gpt-4o\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Query for the agent\n",
    "\n",
    "print(await agent.run(\"Can you tell me which women were awarded the Physics Nobel Prize\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
